#include "notepadwindow.h"
#include "ui_notepadwindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QtDebug>
#include <QDebug>
#include <QToolBar>
#include <QFileDialog>
#include <QFile>
#include <QByteArray>
#include <QTextCodec>
#include <QClipboard>
#include <QFontDialog>

notePadWindow::notePadWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::notePadWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("记事本");

    //菜单栏
    //建立一个菜单栏指针
    QMenuBar *mBar = this->menuBar();
    //对菜单栏添加菜单项
    QMenu *menuFile = mBar->addMenu("文件(F)");
    QMenu *menuEdit = mBar->addMenu("编辑(E)");
    QMenu *menuFormat = mBar->addMenu("格式(O)");
    QMenu *menuHelp = mBar->addMenu("帮助(H)");

    //添加菜单动作
    //文件(F):
    QAction *actionNew = menuFile->addAction("新建(N)");
    QAction *actionClose = menuFile->addAction("关闭(C)");
    QAction *actionOpen = menuFile->addAction("打开(O)");
    QAction *actionSave = menuFile->addAction("保存(S)");
    //编辑(E)
    QAction *actionUndo = menuEdit->addAction("撤销(U)");
    QAction *actionDelete = menuEdit->addAction("删除(D)");
    QAction *actionCut = menuEdit->addAction("剪切(T)");
    QAction *actionCopy = menuEdit->addAction("复制(C)");
    QAction *actionPaste = menuEdit->addAction("粘贴(P)");
    //格式(O)
    QAction *actionTypeFace = menuFormat->addAction("字体(F)");
    QAction *actionWordWrap = menuFormat->addAction("自动换行");
    //帮助(H)
    QAction *actionHelp = menuHelp->addAction("关于(A)");

    //信号与槽函数
    //文件
    connect(actionNew,SIGNAL(triggered(bool)),this,SLOT(file_new()));
    connect(actionClose,SIGNAL(triggered(bool)),this,SLOT(file_close()));
    connect(actionOpen,SIGNAL(triggered(bool)),this,SLOT(file_open()));
    connect(actionSave,SIGNAL(triggered(bool)),this,SLOT(file_save()));
    //编辑
    connect(actionUndo,SIGNAL(triggered(bool)),this,SLOT(edit_undo()));
    connect(actionDelete,SIGNAL(triggered(bool)),this,SLOT(edit_delete()));
    connect(actionCut,SIGNAL(triggered(bool)),this,SLOT(edit_cut()));
    connect(actionCopy,SIGNAL(triggered(bool)),this,SLOT(edit_copy()));
    connect(actionPaste,SIGNAL(triggered(bool)),this,SLOT(edit_paste()));
    //格式
    connect(actionTypeFace,SIGNAL(triggered(bool)),this,SLOT(format_typeface()));
    connect(actionWordWrap,SIGNAL(triggered(bool)),this,SLOT(format_wordwrap()));
    //帮助
    connect(actionHelp,SIGNAL(triggered(bool)),this,SLOT(help_help()));

}

notePadWindow::~notePadWindow()
{
    delete ui;
}

//新建
void notePadWindow::file_new()
{
    ui->textEdit->clear();
    this->setWindowTitle("新建");
    qDebug() << "TextEdit clear\n";
}

//关闭窗口
void notePadWindow::file_close()
{
    ui->textEdit->clear();
    this->close();
    qDebug() << "Window closed\n";
}

//文件打开
void notePadWindow::file_open()
{
    QString pathOutput = QFileDialog::getOpenFileName(this,"open","../","txt(*.txt)");
    bool isEmpty = pathOutput.isEmpty();  //判断文件是否为空
    if(false == isEmpty)
    {
        //不为空，开始打开文件
        QFile *file = new QFile(pathOutput);
        //打开文件
        bool isOk = file->open(QIODevice::ReadOnly); //判断文件是否打开
        if(true == isOk)
        {
            qDebug() << pathOutput << " has opened\n";
            QTextCodec *codec = QTextCodec::codecForName("gbk");
            QByteArray arrayOutput;
            bool isEnd = file->atEnd();
            while(false == isEnd)
            {
                arrayOutput += file->readLine(); //逐行读取
            }
            ui->textEdit->setText(codec->toUnicode(arrayOutput));
        }
        else
        {
            //文件未打开
            qDebug() << pathOutput << " not open!\n";
        }
        file->close();
    }
    else
    {
        //文件为空
        qDebug() << pathOutput << " is empty!\n";
    }
}

//文件保存
void notePadWindow::file_save()
{
    QString pathSave = QFileDialog::getSaveFileName(this,"save","../","txt(*.txt)");
    bool isEmpty = pathSave.isEmpty();
    //如果路径存在则保存
    if(false == isEmpty)
    {
        QFile *file = new QFile();
        file->setFileName(pathSave);
        //以只写方式打开文件
        bool isOk = file->open(QIODevice::WriteOnly);
        if(true == isOk)
        {
            qDebug() << pathSave << " saved\n";
            QString strSave = ui->textEdit->toPlainText();
            file->write(strSave.toLocal8Bit());
        }
        else
        {
            qDebug() << pathSave << " failed open\n";
        }
        file->close();
    }
    else
    {
        qDebug() << pathSave << " not exist!\n";
    }
}

//撤销
void notePadWindow::edit_undo()
{
    ui->textEdit->undo();
    qDebug() << "opr undo\n";
}

//删除
void notePadWindow::edit_delete()
{
    QTextCursor thisDelcursor = ui->textEdit->textCursor();
    thisDelcursor.deleteChar();
    qDebug() << "opr delete\n";
}

//剪切
void notePadWindow::edit_cut()
{
    QClipboard *thisClipboard = QApplication::clipboard();
    thisClipboard->clear();
    ui->textEdit->cut();
    QString thisSelect = thisClipboard->text();
    qDebug() << "opr cut:" << thisSelect << "\n";
}

//复制
void notePadWindow::edit_copy()
{
    ui->textEdit->copy();
    qDebug() << "opr copy\n";
}

//粘贴
void notePadWindow::edit_paste()
{
    ui->textEdit->paste();
    qDebug() << "opr paste\n";
}

//设置字体
void notePadWindow::format_typeface()
{
    bool isOk;
    QFont thisFont = QFontDialog::getFont(&isOk);
    if(isOk)
    {
        ui->textEdit->setFont(thisFont);
    }
    qDebug() << "Font setted\n";
}

//设置自动换行
void notePadWindow::format_wordwrap()
{
    ui->textEdit->setWordWrapMode(QTextOption::WordWrap);
    qDebug() << "Wordwrap setted\n";
}

//关于
void notePadWindow::help_help()
{
    helpDialog *aboutpage = new helpDialog(this);
    aboutpage->setWindowTitle("About");
    aboutpage->setModal(true);
    aboutpage->show();
}




